package com.servlet.control;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import com.bean.User;
import com.jdbc.util.StudentSelectDB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 学生选课功能设计
 */
@WebServlet("/StudentSelectServlet")
public class StudentSelectServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentSelectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    /**
     * 响应学生选课
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * 响应学生选课
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //获取学生姓名
        User user = (User) request.getSession().getAttribute("user");
        String userName = user.getUsername();
        //获取学生想要选的课
        String []checkList=request.getParameterValues("selectCourse");

        StudentSelectDB db=new StudentSelectDB();
        db.getConnection();

        //学生选课，并获取选课失败的课程名信息
        try {
            Boolean isflagSelecteFlag=false;//标志是否已经选了刚刚勾选的课程
            String isSelectedMessage = "";//提示已经选择的课程信息
            List<String> isSelectedCoursesList = db.getIsSelectedCourse(userName, checkList);//获取已经选择的课程信息
            //判断本次选的课是否已经选了
            if (isSelectedCoursesList.size() > 0)
            {
                for (String isSelectedCourseName : isSelectedCoursesList)
                {
                    isSelectedMessage = isSelectedMessage + "《"+isSelectedCourseName + "》";
                }
                isSelectedMessage=isSelectedMessage + "课程您已选，不可重复选择！";
                isflagSelecteFlag=true;
            }
            //学生选课，selectCourse()返回的是没有容量的课程名List
            List<String> failCoursesList = db.selectCourse(userName, checkList);
            //若学生要选的课中有的课已经没有容量
            if (failCoursesList.size() > 0)
            {
                String failMessage = "";
                for (String failCourseName : failCoursesList)
                {
                    failMessage = failMessage + "《" + failCourseName + "》";
                }
                request.setAttribute("selectMessage", "课程名为：" + failMessage + "的课程已经被选完！");
            }
            //若学生要选的课都有容量，则判断是否有在之前已经选择的课
            else
            {
                if(isflagSelecteFlag)
                {
                    request.setAttribute("selectMessage", isSelectedMessage);
                }
                else
                {
                    request.setAttribute("selectMessage", "选课成功!" );
                }
            }
        }catch(Exception e)
        {
            //学生没有勾选课程
            request.setAttribute("selectMessage", "请勾选您要选的课！");
        }
        //跳转到学生选的课页面
        request.getRequestDispatcher("/jsp/selectCourse.jsp").forward(request, response);
        db.release();
    }

}
